package com.tbsoft.ehr.action.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.login.LoginInfo;
import com.tbsoft.ehr.login.RoleInfo;
import com.tbsoft.ehr.login.UserInfo;
import com.tbsoft.ehr.service.user.UserService;
import com.tbsoft.ehr.util.ConfigProps;

/**
 * @desc 用户管理
 * @author vidy
 *
 */
public class UserAction extends BaseAction{
	private Log log = LogFactory.getLog(getClass());
	private UserService userService;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private UserInfo user;
	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<UserInfo> listUser = new ArrayList<UserInfo>();
	
	public List<UserInfo> getListUser() {
		return listUser;
	}

	public void setListUser(List<UserInfo> listUser) {
		this.listUser = listUser;
	}

	//密码修改相关字段
	private String oldPwd;
	private String newPwd;
	
	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String list(){
		listUser.clear();
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();
		try{
			List<UserInfo> tmpList = userService.getUserList(user, startIndex, getNumPerPage());
			listUser.addAll(tmpList);
		}catch(Exception e){
			log.error("list exception", e);
			setStatusCode("300");
			setMessage(e.getMessage());
		}
		return "browse";
	}
	public String delete(){
		try{
			userService.deleteUser(user);
		}catch(Exception e){
			log.error("delete exception", e);
			setStatusCode("300");
			setMessage("删除用户失败");
		}
		setRel("user_browse");
		return SUCCESS;
	}
	public String save(){
		try{
			userService.saveUser(user,roleCheckList);
		}catch(Exception e){
			log.error("save exception", e);
			setStatusCode("300");
			setMessage("新增用户错误，请确认用户Id是否已存在。");
		}
		setCallbackType("closeCurrent");
		setRel("user_browse");
		return SUCCESS;
	}

	public String changePwd() {
		return "changePwd";
	}
	
	public String updatePwd() {
		LoginInfo loginInfo = (LoginInfo)ActionContext.getContext().getSession().get("loginInfo");
		this.user = this.userService.getUser(loginInfo.getUserId());
		
		String decodeOldPwd = this.userService.getDecodePassword(this.oldPwd);
		if(decodeOldPwd.equals(this.user.getPassword())) {
			try {
				String decodeNewPwd = this.userService.getDecodePassword(this.newPwd);
				this.user.setPassword(decodeNewPwd);
				this.userService.updateUserPwd(this.user);
				return SUCCESS;
			} catch(Exception ex) {
				log.error("密码修改失败",ex);
				return errorReturn("密码修改过程中产生异常，修改失败");
			}			
		} else {
			log.error("提供的原始密码不匹配！");
			return errorReturn("原始密码不正确，密码修改失败");
		}
	}
	
	public String resetPasswd(){
		try{
			this.user = this.userService.getUser(user.getUserId());
			String decodePwd = this.userService.getDecodePassword(user.getUserId());
			user.setPassword(decodePwd);
			userService.updateUserPwd(user);
		}catch(Exception e){
			log.error("resetPasswd exception:",e);
			setStatusCode("300");
			setMessage("密码重置失败");
		}
		return SUCCESS;
	}
	private List<RoleInfo> roleList;
	public List<RoleInfo> getRoleList() {
		return roleList;
	}

	public String openRole(){
		user = userService.getUser(user.getUserId());
		roleList = userService.getAllRoleList();
		for(RoleInfo ri :user.getRoleSet()){
			for(RoleInfo ari:roleList){
				if(ri.getRoleId().equals(ari.getRoleId())){
					ari.setSelect(true);
				}
			}
		}
		return "userRole";
	}
	private List<String> roleCheckList = new ArrayList<String>();
	public List<String> getRoleCheckList() {
		return roleCheckList;
	}

	public void setRoleCheckList(List<String> roleCheckList) {
		this.roleCheckList = roleCheckList;
	}
	public String role(){
		try{
			userService.saveUserRole(user.getUserId(), roleCheckList);
		}catch(Exception e){
			log.error("role exception", e);
			setStatusCode("300");
			setMessage("角色授权失败");
		}
		setRel("user_browse");
		return SUCCESS;
	}
	@Override
	public String add() {
		roleList = userService.getAllRoleList();
		return super.add();
	}
}
